//
//  LYZNewfeatureViewController.m
//  柳州微博
//
//  Created by Apple_Lzzy40 on 16/9/28.
//  Copyright © 2016年 Apple_Lzzy40. All rights reserved.
//

#import "ZQNewfeatureViewController.h"
#import "ZQViewController.h"
#define ZQNewfeatureCount 4
@interface ZQNewfeatureViewController ()<UIScrollViewDelegate>
@property(nonatomic,weak) UIPageControl *pageControl;

@property(nonatomic,weak) UIScrollView *scrollView;

@end

@implementation ZQNewfeatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.创建一个scrollView：显示所有的新特性图片
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    // 2.添加图片到scrollView中
    CGFloat scrollW = scrollView.width;
    CGFloat scrollH = scrollView.height;
    for (int i = 0; i<ZQNewfeatureCount; i++) {
       
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.width = scrollW;
        imageView.height = scrollH;
        imageView.y = 0;
        imageView.x = i * scrollW;
        // 显示图片
        NSString *name = [NSString stringWithFormat:@"new_feature_%d", i + 1];
        imageView.image = [UIImage imageNamed:name];
        [scrollView addSubview:imageView];
        
        // 如果是最后一个imageView，就往里面添加其他内容
        if (i == ZQNewfeatureCount - 1) {
            //[self setupLastImageView:imageView];
            [self setupLastImageView:imageView];
                    }
    }
    // 3.设置scrollView的其他属性
    // 如果想要某个方向上不能滚动，那么这个方向对应的尺寸数值传0即可
    scrollView.contentSize = CGSizeMake(ZQNewfeatureCount * scrollW, 0);
    scrollView.bounces = NO; // 去除弹簧效果
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;

    // 4.添加pageControl：分页，展示目前看的是第几页
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = ZQNewfeatureCount;
    pageControl.backgroundColor = [UIColor redColor];
    pageControl.currentPageIndicatorTintColor = ZQColor(253, 98, 42);
    pageControl.pageIndicatorTintColor = ZQColor(189, 189, 189);
    pageControl.centerX = scrollW * 0.5;
    pageControl.y = scrollH - 50;
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    double page=scrollView.contentOffset.x/scrollView.width;
    //四舍五入计算出页码
    self.pageControl.currentPage = (int)(page + 0.5);
    // 1.3四舍五入 1.3 + 0.5 = 1.8 强转为整数(int)1.8= 1
    // 1.5四舍五入 1.5 + 0.5 = 2.0 强转为整数(int)2.0= 2
    // 1.6四舍五入 1.6 + 0.5 = 2.1 强转为整数(int)2.1= 2
    // 0.7四舍五入 0.7 + 0.5 = 1.2 强转为整数(int)1.2= 1

}

-(void)setupLastImageView:(UIImageView *)imageView
{

    // 开启交互功能
    imageView.userInteractionEnabled = YES;
    
    // 1.分享给大家（checkbox）
    UIButton *shareBtn = [[UIButton alloc] init];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    [shareBtn setTitle:@"显示新特性" forState:UIControlStateNormal];
    [shareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    shareBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    shareBtn.width = 200;
    shareBtn.height = 30;
    shareBtn.centerX = imageView.width * 0.5;
    shareBtn.y= imageView.height * 0.65;
    [shareBtn addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:shareBtn];
    
     shareBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    NSString *key = @"BundleVersion";
    // 上一次的使用版本（存储在沙盒中的版本号）
    BOOL lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    shareBtn.selected = !lastVersion;
    // 2.开始微博
    UIButton *startBtn = [[UIButton alloc] init];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    startBtn.size = startBtn.currentBackgroundImage.size;
    startBtn.centerX = shareBtn.centerX;
    startBtn.y = imageView.height * 0.75;
    [startBtn setTitle:@"开始微博" forState:UIControlStateNormal];
    [startBtn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startBtn];

}

- (void)shareClick:(UIButton *)shareBtn
{
    // 状态取反
    shareBtn.selected = !shareBtn.isSelected;
     NSString *key = @"BundleVersion";
    // 将当前的版本号存进沙盒
        [[NSUserDefaults standardUserDefaults] setBool:!shareBtn.isSelected forKey:key];
        [[NSUserDefaults standardUserDefaults] synchronize];
   // NSLog(@"%d",!shareBtn.isSelected);
    
}
-(void)startClick
{
     // 切换到HWTabBarController
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    window.rootViewController=[[ZQViewController alloc] init];
}

- (void)dealloc
{
    ZQLog(@"ZQNewfeatureViewController-dealloc");
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
